home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Converters / Convert_MacPaint / Source / shared.subproj / RCS / PSFile.m,v < prev    next >
Text File  |  1995-06-12  |  10KB  |  338 lines

  1. head     1.4;
  2. branch   ;
  3. access   ;
  4. symbols  beta10:1.3;
  5. locks    death:1.4;
  6. comment  @@;
  7.  
  8.  
  9. 1.4
  10. date     93.04.04.23.44.52;  author death;  state Exp;
  11. branches ;
  12. next     1.3;
  13.  
  14. 1.3
  15. date     93.01.10.15.08.35;  author death;  state Exp;
  16. branches ;
  17. next     1.2;
  18.  
  19. 1.2
  20. date     92.07.26.13.59.51;  author death;  state Exp;
  21. branches ;
  22. next     1.1;
  23.  
  24. 1.1
  25. date     92.07.26.13.54.11;  author death;  state Exp;
  26. branches ;
  27. next     ;
  28.  
  29.  
  30. desc
  31. @Basic ps file
  32. @
  33.  
  34.  
  35. 1.4
  36. log
  37. @Sun Apr  4 23:44:52 PDT 1993
  38. @
  39. text
  40. @#import "PSFile.h"
  41. #import<stdio.h>
  42. #import <stdlib.h>
  43. #import <string.h>
  44. #include <stdarg.h>    // For variable arguments stuff
  45.  
  46. @@implementation PSFile
  47.  
  48. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. //    Method:    writeDCDcomment:
  50. //    Parameters:    the line of text we are to write as a comment
  51. //    Returns:     a Reply
  52. //    Description:
  53. //        This writes the specified string to the PS file, following it with a newline, and
  54. //        preceeding it immediately with a %% so it appears as a PS DCD comment
  55. //        It does no formatting of the line, as some other smarter routine should.
  56. //    Note:
  57. //        This blindly assumes that it first thing on a new line.  A good PS file object
  58. //        should be keeping track of these things, and jump to a new line if we aren't.
  59. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. - WriteDSCComment: (CString) comment
  61. {
  62.     CString    output = NewCString(strlen(comment) + 4);
  63.  
  64.     sprintf(output, "%%%%%s\n", comment);    //That's, two percents and a string. =)
  65.     [self Write: strlen(output) BytesFrom: (ByteString) output];
  66.     FreeCString(output);
  67.     return self;
  68. }
  69.  
  70.  
  71. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  72. //    Method:        WriteDSCCommentUsing:WithFormat:
  73. //    Parameters:    A buffer provided by the caller, a format, and N arguments to be written
  74. //    Returns:     self
  75. //    Stores:        nothing (bug)
  76. //    Description:
  77. //        This writes out a text string given variable arguments, as a DSC comment.
  78. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  79. - WriteDSCCommentUsing: (CString) buffer WithFormat: (CString) format, ...;
  80. {
  81.     va_list parameter_list;
  82.     CString    output;
  83.  
  84.     va_start(parameter_list, format);
  85.  
  86.     vsprintf(buffer, format, parameter_list);     // doc implies this does a va_end
  87.  
  88.     output = NewCString(strlen(buffer) + 4);
  89.     sprintf(output, "%%%%%s\n", buffer);    //That's, two percents and a string. =)
  90.     [self Write: strlen(output) BytesFrom: (ByteString) output];
  91.     FreeCString(output);
  92.     return self;
  93. }
  94.  
  95.  
  96. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  97. //    Method:    WriteComment:
  98. //    Parameters:    the line of text we are to write as a comment
  99. //    Returns:     a Reply
  100. //    Description:
  101. //        This writes the specified string to the PS file, following it with a newline, and
  102. //        preceeding it immediately with a %.
  103. //        It does no formatting of the line, as some other smarter routine should.
  104. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  105. - WriteComment: (CString) comment
  106. {
  107.     CString    output = NewCString(strlen(comment) + 3);
  108.  
  109.     sprintf(output, "%%%s\n", comment);    //That's, one percent and a string. =)
  110.     [self Write: strlen(output) BytesFrom: (ByteString) output];
  111.     FreeCString(output);
  112.     return self;
  113. }
  114.  
  115.  
  116. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  117. //    Method:        WriteCommentUsing:WithFormat:
  118. //    Parameters:    A buffer provided by the caller, a format, and N arguments to be written
  119. //    Returns:     self
  120. //    Stores:        nothing (bug)
  121. //    Description:
  122. //        This writes out a text string given variable arguments, as a PS comment.
  123. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  124. - WriteCommentUsing: (CString) buffer WithFormat: (CString) format, ...;
  125. {
  126.     va_list parameter_list;
  127.     CString    output;
  128.     
  129.     va_start(parameter_list, format);
  130.  
  131.     vsprintf(buffer, format, parameter_list);      // doc implies this does a va_end
  132.  
  133.     output = NewCString(strlen(buffer) + 2);
  134.     sprintf(output, "%%%s\n", buffer);    //That's, 1 percent and a string.
  135.     [self Write: strlen(output) BytesFrom: (ByteString) output];
  136.     FreeCString(output);
  137.     return self;
  138. }
  139.  
  140.  
  141.  
  142.  
  143. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  144. //    Method:    writePSline:
  145. //    Parameters:    the line of text we are to write
  146. //    Returns:     a Reply
  147. //    Description:
  148. //        This writes the specified string to the PS file, following it with a newline.
  149. //        It does no formatting of the line, as some other smarter routine should.
  150. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  151. - WritePSLine: (CString) theLine
  152. {
  153.     CString    output = NewCString(strlen(theLine) + 2);
  154.  
  155.     sprintf(output, "%s\n", theLine);
  156.     [self Write: strlen(output) BytesFrom: (ByteString) output];
  157.     FreeCString(output);
  158.     return self;
  159. }
  160.  
  161.  
  162. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  163. //    Method:        WritePSLineUsing:WithFormat:
  164. //    Parameters:    A buffer provided by the caller, a format, and N arguments to be written
  165. //    Returns:     self
  166. //    Stores:        nothing (bug)
  167. //    Description:
  168. //        This writes out a text string given variable arguments
  169. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  170. - WritePSLineUsing: (CString) buffer WithFormat: (CString) format, ...;
  171. {
  172.     va_list parameter_list;
  173.     CString    output;
  174.     
  175.     va_start(parameter_list, format);
  176.  
  177.     vsprintf(buffer, format, parameter_list);      // doc implies this does a va_end
  178.  
  179.     output = NewCString(strlen(buffer) + 1);
  180.     sprintf(output, "%s\n", buffer);
  181.     [self Write: strlen(output) BytesFrom: (ByteString) output];
  182.     FreeCString(output);
  183.     return self;
  184. }
  185.  
  186.  
  187. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  188. //    Method:    write:BytesOfHexData:
  189. //    Parameters:    The number of Bytes to write
  190. //                A buffer of binary data
  191. //    Returns:     self
  192. //    Description:
  193. //        This method writes out num Bytes of data from the buffer specified by buffer.
  194. //        buffer is not modified.  Each Byte is converted to a pair of hexidcecimal digits,
  195. //        and these are written out.  When the whole buffer has been written, a newline
  196. //        is added.
  197. //        The intended use for this method is for dumping raw image data to be used
  198. //        with PS operators like (surprisingly) image.
  199. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  200. - Write: (PositiveInteger) num BytesOfHexDataFrom: (ByteString) buffer
  201. {
  202.     PositiveInteger    index;
  203.     for (index = 0; index < num; index++)
  204.         [self    WriteByteAsHex: buffer[index]];
  205.     [self ForceNewLine];
  206.     return self;
  207. }
  208.  
  209.  
  210. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  211. //    Method:    write:InvertedBytesOfHexDataFrom:
  212. //    Parameters:    The number of Bytes to write
  213. //                A buffer of binary data
  214. //    Returns:     self
  215. //    Description:
  216. //        This method writes out num Bytes of data from the buffer specified by buffer.
  217. //        buffer is not modified.  Each Byte is converted to a pair of hexidcecimal digits,
  218. //        and these are written out with a newline following all.
  219. //        During the process, the Byte is inverted (thus 00 becomes FF, etc.
  220. //        The intended use for this method is for dumping raw image data to be used
  221. //        with PS operators like (surprisingly) image.
  222. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  223. - Write: (PositiveInteger) num InvertedBytesOfHexDataFrom: (ByteString) buffer
  224. {
  225.     PositiveInteger index;
  226.     for (index = 0; index < num; index++)
  227.         [self    WriteByteAsHex: ~buffer[index]];
  228.     [self ForceNewLine];
  229.     return self;
  230. }
  231.  
  232.  
  233. - ForceNewLine
  234. {
  235.     return [self WriteByte: (Byte) '\n'];
  236.     return self;
  237. }
  238.  
  239.  
  240. - WriteByteAsHex: (Byte) theByte
  241. {
  242.     Byte        nibble1 = theByte >> 4;
  243.     Byte        nibble2 = (theByte & 0x0F);
  244.         
  245.     if (nibble1 < 10)
  246.         [self WriteByte: (Byte) '0'+nibble1]; // digit 0-9
  247.     else
  248.         [self WriteByte: (Byte) 'A'+nibble1-10]; // cap A-F
  249.  
  250.     if (nibble2 < 10)
  251.         [self WriteByte: (Byte) '0'+nibble2]; // digit 0-9
  252.     else
  253.         [self WriteByte: (Byte) 'A'+nibble2-10]; // cap A-F
  254.     return self;
  255. }
  256.  
  257.  
  258. @@end@
  259.  
  260.  
  261. 1.3
  262. log
  263. @Sun Jan 10 15:08:35 PST 1993
  264. @
  265. text
  266. @@
  267.  
  268.  
  269. 1.2
  270. log
  271. @updated psfile
  272. @
  273. text
  274. @a8 15
  275.  
  276. - AppendFrom: sourceFile
  277. {
  278.     Integer size;
  279.     ByteString    buffer;
  280.  
  281.     size = [sourceFile FileSize];
  282.     buffer = NewByteString(size);
  283.     
  284.     [sourceFile Read: size BytesInto: buffer];
  285.     [self Write: size BytesFrom: buffer];
  286.     FreeByteString(buffer);
  287.     return self;
  288. }
  289.  
  290. d156 2
  291. a157 1
  292. //        and these are written out in groups of 72 characters, followed by a newline.
  293. d163 1
  294. a163 1
  295.     PositiveInteger index, linecnt=0;
  296. a164 2
  297.     {
  298.         linecnt++;
  299. d166 1
  300. a166 8
  301.         if (linecnt == 36)
  302.         {
  303.             linecnt = 0;
  304.             [self ForceNewLine];
  305.         }
  306.     }
  307.     if (linecnt != 0)
  308.         [self ForceNewLine];
  309. d179 2
  310. a180 2
  311. //        and these are written out in groups of 72 characters, followed by a newline.
  312. //        During the process, the Byte is inverted (thus 0 becomes F, etc.
  313. d186 1
  314. a186 1
  315.     PositiveInteger index, linecnt=0;
  316. a187 2
  317.     {
  318.         linecnt++;
  319. d189 1
  320. a189 8
  321.         if (linecnt == 36)
  322.         {
  323.             linecnt = 0;
  324.             [self ForceNewLine];
  325.         }
  326.     }
  327.     if (linecnt != 0)
  328.         [self ForceNewLine];
  329. @
  330.  
  331.  
  332. 1.1
  333. log
  334. @Initial revision
  335. @
  336. text
  337. @@
  338.